<?php
               
$path = realpath(dirname(__FILE__));                                  
require_once ("$path/../utils/mysqli/MysqliDb.php");


abstract class Express {
    public abstract  function sql();
} ;


class Conditions extends Express {
    protected $list = array();

    public function one($name){
        $new1 = new LogicExpress($name);
        $this->list [] = $new1 ;
        return $new1 ;
    }
    public function sql(){
        if( !$this->list ) return '' ;
     
        $size = count($this->list);

        if(  $size == 0 ) return '';


        $sql  = '(';
        $subs = array();
        for ($i=0; $i < $size ; $i++) { 
            $element = $this->list[$i];
            $sub = $element->sql();
            $subs [] = $sub ;
        }

        $sql = $sql . join( " and " , $sub) ;

        $sql = $sql . ')' ;
        return $sql;
 
    }
    public function and(){
        return $this->one("and");
    }
    public function or(){
        return $this->one("or");
    }
};
class Simplexpress extends Express {
    protected  $name ;
    protected  $opt ;
    protected  $value;

    function __construct( $name , $opt , $value ) {
        $this->name = $name ;
        $this->opt = $opt;
        $this->value = $value;
    }

    static function wrapValue($value){
        $type = gettype($value);
        print("type is $type<br/>");
        if( $type == 'string')
            return "'" . $value ."'" ;
        elseif ($type == "integer") {
            return $value;
        }
        return $value;
    }

    public function sql(){
        return " ( $this->name $this->opt " . Simplexpress::wrapValue($this->value) . " )";
    }
}
class LogicExpress extends Express {

    protected $opt ;

    protected $exps = array();
    function or ( ){
        $exp = new LogicExpress("or");
        $this->addExpress($exp);
        return $exp;   
    }
    function addExpress( $obj  )
    {
        if(!($obj instanceof Express) ) 
            throw new Exception("Must be subclass of Express : " . get_class($obj));
            
        $this->exps [] = $obj;
    //    return $this;
    }
    public function eq( $name , $value )
    {
        $exp = new Simplexpress($name, "=" , $value);
        $this->addExpress($exp);
        return $this;
    }
    public function add( $name , $opt, $value )
    {
        $exp = new Simplexpress($name, $opt, $value);
        $this->addExpress($exp);
        return $this;
    }

    

    public function sql(){

        if( !$this->exps ) return '' ;

        $size = count($this->exps);

        if(  $size == 0 ) return '';

        $sql  = '(';
        $subs = array();
        for ($i=0; $i < $size ; $i++) { 
            $element = $this->exps[$i];
            $sub = "( " .  $element->sql() . " )" ;
            $subs [] = $sub ;
        }

        $sql = $sql . join( " ". $this->opt . " " , $subs ) ;

        $sql = $sql . ')' ;
        return $sql;

    }

    function __construct($opt ) {
        $this->opt = $opt;
    }
} 


class BaseDao{

    protected $db = null ;

    function __construct(){
     //   print("in __construct\n");
        $this->db = MysqliDb::getInstance();
        if( $this->db == null )
            $this->db = new MysqliDb (DBHOST,USER,PASSWORD , DATABASE);

     //   print("in __construct 2 \n" . ($this->db != null ? 'not null' : 'null') );
    }


    /**
     * 
     * @return [type]          [description]
     */
    static function bindWhereConds(  $params ,  $order=null , $begin =0 , $size = 100 )
    {

        $sql = " ";   
        
        if($params && count($params)>0){
            $sql = $sql . "  WHERE  " ;            
            $list = array();
            foreach ($params as $key => $value) {
                $sub = " ( $key $value ) ";
                $list[] = $sub;
            }

            $subs = join(" and " , $list);
            $sql = $sql . $subs ;            

        }

        if( $order !=null  ){
                $asc = " ASC " ;
                if(  $order[0] == '-')
                {
                    $asc=" DESC ";
                    $order = substr( $order , 1);
                }
                $sql = $sql . " ORDER BY $order $asc " ;
        }
        if($size !=null && $size > 0 ){
            $sql = $sql . " LIMIT $begin , $size ";
        }

        return $sql;
    }

    /**
     * 
     * @return [type]          [description]
     */
    static function bindParams( $table, 
         $colums=null,  $params ,  $order=null , $begin =0 , $size = 100 )
    {

        $sql = " SELECT ";   
        if( $colums == null ){
            $sql = $sql . "  * " ;
        }else {
            $cols = join("," , $columns);
            $sql = $sql . $cols . " " ;
        }

        $sql = $sql . "  FROM  " . $table . " ";   

        return $sql . BaseDao::bindWhereConds( $params , $order , $begin , $size); 

        // if($params && count($params)>0){
        //     $sql = $sql . "  WHERE  " ;            
        //     $list = array();
        //     foreach ($params as $key => $value) {
        //         $sub = " ( $key $value ) ";
        //         $list[] = $sub;
        //     }

        //     $subs = join(" and " , $list);
        //     $sql = $sql . $subs ;            

        // }

        // if( $order !=null  ){
        //         $asc = " ASC " ;
        //         if(  $order[0] == '-')
        //         {
        //             $asc=" DESC ";
        //             $order = substr( $order , 1);
        //         }
        //         $sql = $sql . " ORDER BY $order $asc " ;
        // }
        // if($size !=null && $size > 0 ){
        //     $sql = $sql . " LIMIT $begin , $size ";
        // }

        // return $sql;
    }
}

 


?> 